package com.bulletelc.apollopre.module.main.cart;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.CartListBean;
import com.bulletelc.apollopre.interfaces.listener.OnCartCheckBoxChangeListener;
import com.bulletelc.apollopre.interfaces.listener.OnCartCountChangeListener;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;
import com.bulletelc.apollopre.module.display.product.details.ProductDetailsActivity;
import com.bulletelc.apollopre.utils.MyUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/19
 * desc  : 购物车列表的adapter
 */

public class CartListAdapter extends BaseExpandableListAdapter implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private Context context;
    private List<CartListBean> cartListBeans;
    private LayoutInflater inflater;
    private OnCartCountChangeListener countChangeListener;
    private OnCartCheckBoxChangeListener checkBoxChangeListener;

    public CartListAdapter(Context context, List<CartListBean> cartListBeans) {
        this.context = context;
        this.cartListBeans = cartListBeans;
        this.inflater = LayoutInflater.from(context);
    }

    /**
     * 设置数据源(过滤掉空child数据)
     *
     * @param cartListBeans
     */
    public void setCartListBeans(List<CartListBean> cartListBeans) {
        List<CartListBean> filterCartLists = new ArrayList<>();
        if (cartListBeans != null) {
            for (int i = 0, size = cartListBeans.size(); i < size; i++) {
                CartListBean cartListBean = cartListBeans.get(i);
                if (cartListBean.getProduct_list().size() > 0) {
                    filterCartLists.add(cartListBean);
                }
            }
            this.cartListBeans = filterCartLists;
        } else {
            cartListBeans = new ArrayList<>();
            this.cartListBeans = cartListBeans;
        }
    }

    public void setCountChangeListener(OnCartCountChangeListener countChangeListener) {
        this.countChangeListener = countChangeListener;
    }

    public void setCheckBoxChangeListener(OnCartCheckBoxChangeListener checkBoxChangeListener) {
        this.checkBoxChangeListener = checkBoxChangeListener;
    }

    @Override
    public int getGroupCount() {
        if (cartListBeans != null) {
            return cartListBeans.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (cartListBeans != null) {
            return cartListBeans.get(groupPosition).getProduct_list().size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return cartListBeans.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return cartListBeans.get(groupPosition).getProduct_list().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderParent holderParent;
        if (convertView == null) {
            holderParent = new ViewHolderParent();
            convertView = inflater.inflate(R.layout.cart_parent_layout, parent, false);
            holderParent.cbCartParent = convertView.findViewById(R.id.cbCartParent);
            holderParent.tvCartParent = convertView.findViewById(R.id.tvCartParent);

            holderParent.cbCartParent.setOnClickListener(this);

            convertView.setTag(holderParent);
        } else {
            holderParent = (ViewHolderParent) convertView.getTag();
        }
        holderParent.cbCartParent.setTag(R.id.tag_cart_cb_parent, groupPosition);

        holderParent.tvCartParent.setText(cartListBeans.get(groupPosition).getSupplier_name());
        holderParent.cbCartParent.setChecked(cartListBeans.get(groupPosition).isChecked());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        CartListBean.CartProductListBean cartProductListBean = cartListBeans.get(groupPosition).getProduct_list().get(childPosition);
        ViewHolderChild holderChild;
        if (convertView == null) {
            holderChild = new ViewHolderChild();
            convertView = inflater.inflate(R.layout.cart_child_layout, parent, false);
            holderChild.llItem = convertView.findViewById(R.id.llItem);
            holderChild.cbCartChild = convertView.findViewById(R.id.cbCartChild);
            holderChild.ivProductImg = convertView.findViewById(R.id.ivProductImg);
            holderChild.tvProductName = convertView.findViewById(R.id.tvProductName);
            holderChild.tvSku = convertView.findViewById(R.id.tvSku);
            holderChild.tvProductPrice = convertView.findViewById(R.id.tvProductPrice);
            holderChild.tvProductNumShow = convertView.findViewById(R.id.tvProductNumShow);
            holderChild.iBtnSubtractNum = convertView.findViewById(R.id.iBtnSubtractNum);
            holderChild.tvProductNum = convertView.findViewById(R.id.tvProductNum);
            holderChild.iBtnAddNum = convertView.findViewById(R.id.iBtnAddNum);
            holderChild.tvInstallFee = convertView.findViewById(R.id.tvInstallFee);

            holderChild.iBtnAddNum.setOnClickListener(this);
            holderChild.iBtnSubtractNum.setOnClickListener(this);
            holderChild.cbCartChild.setOnClickListener(this);
            holderChild.llItem.setOnClickListener(this);

            convertView.setTag(holderChild);
        } else {
            holderChild = (ViewHolderChild) convertView.getTag();
        }
        holderChild.llItem.setTag(R.id.tag_cart_group, groupPosition);
        holderChild.llItem.setTag(R.id.tag_cart_child, childPosition);
        holderChild.cbCartChild.setTag(R.id.tag_cart_cb_child_group, groupPosition);
        holderChild.cbCartChild.setTag(R.id.tag_cart_cb_child_child, childPosition);

        holderChild.tvProductName.setText(cartProductListBean.getName());
        holderChild.tvSku.setText(cartProductListBean.getProduct_sku_title());
        holderChild.tvProductPrice.setText("¥" + MyUtil.format2(cartProductListBean.getPrice()));
        holderChild.tvProductNumShow.setText("x" + cartProductListBean.getQuantity());
        holderChild.tvInstallFee.setText("¥" + MyUtil.format2(cartProductListBean.getInstall_fee()));
        holderChild.tvProductNum.setText(String.valueOf(cartProductListBean.getQuantity()));
        holderChild.cbCartChild.setChecked(cartProductListBean.isChecked());
        ImageLoaderManager.getInstance().loadImage(ImageLoaderManager.getDefaultOptions(holderChild.ivProductImg, cartProductListBean.getLogo_url()));

        holderChild.iBtnAddNum.setTag(R.id.tag_cart_group, groupPosition);
        holderChild.iBtnAddNum.setTag(R.id.tag_cart_child, childPosition);
        holderChild.iBtnAddNum.setTag(R.id.tag_cart_count_view, holderChild.tvProductNum);
        holderChild.iBtnSubtractNum.setTag(R.id.tag_cart_group, groupPosition);
        holderChild.iBtnSubtractNum.setTag(R.id.tag_cart_child, childPosition);
        holderChild.iBtnSubtractNum.setTag(R.id.tag_cart_count_view, holderChild.tvProductNum);

        return convertView;
    }

    /**
     * 设置子列表是否可选中,返回true可以选中
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.iBtnAddNum:
                int groupPosition = (int) v.getTag(R.id.tag_cart_group);
                int childPosition = (int) v.getTag(R.id.tag_cart_child);
                if (countChangeListener != null) {
                    countChangeListener.onCountAdd(groupPosition, childPosition, (View) v.getTag(R.id.tag_cart_count_view), cartListBeans.get(groupPosition).getProduct_list().get(childPosition).isChecked());
                }
                break;

            case R.id.iBtnSubtractNum:
                int groupPosition2 = (int) v.getTag(R.id.tag_cart_group);
                int childPosition2 = (int) v.getTag(R.id.tag_cart_child);
                if (countChangeListener != null) {
                    countChangeListener.onCountReduce(groupPosition2, childPosition2, (View) v.getTag(R.id.tag_cart_count_view), cartListBeans.get(groupPosition2).getProduct_list().get(childPosition2).isChecked());
                }
                break;

            case R.id.cbCartParent:
                boolean isCheckedParent=((CheckBox) v).isChecked();
                int groupPosition3 = (int) v.getTag(R.id.tag_cart_cb_parent);

                if (checkBoxChangeListener != null) {
                    checkBoxChangeListener.onGroupCheck(groupPosition3, isCheckedParent);
                }
                break;

            case R.id.cbCartChild:
                boolean isCheckedChild=((CheckBox) v).isChecked();
                int groupPosition4 = (int) v.getTag(R.id.tag_cart_cb_child_group);
                int childPosition4 = (int) v.getTag(R.id.tag_cart_cb_child_child);
                if (checkBoxChangeListener != null) {
                    checkBoxChangeListener.onChildCheck(groupPosition4, childPosition4, isCheckedChild);
                }
                break;

            case R.id.llItem:
                int groupPosition5 = (int) v.getTag(R.id.tag_cart_group);
                int childPosition5 = (int) v.getTag(R.id.tag_cart_child);
                Intent intent = new Intent(context, ProductDetailsActivity.class);
                intent.putExtra(IntentCode.EXTRA_KEY_PRODUCT_ID, cartListBeans.get(groupPosition5).getProduct_list().get(childPosition5).getProduct_id());
                context.startActivity(intent);
                break;

        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cbCartParent:

                break;

            case R.id.cbCartChild:

                break;
        }
    }

    private static class ViewHolderParent {
        CheckBox cbCartParent;
        TextView tvCartParent;
    }

    private static class ViewHolderChild {
        LinearLayout llItem;
        CheckBox cbCartChild;
        ImageView ivProductImg;
        TextView tvProductName;
        TextView tvInstallFee;
        TextView tvSku;
        TextView tvProductPrice;
        TextView tvProductNumShow;
        ImageButton iBtnSubtractNum;
        TextView tvProductNum;
        ImageButton iBtnAddNum;
    }
}
